package com.struts.deal.admin.user;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import com.base.Constants;
import com.ibatis.po.user.TUserInfo;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CSqlMap;
import com.lg.system.CTurnPage;
import com.lg.system.MessageBean;
import com.struts.web.manage.admin.user.EditPassForm;
import com.struts.web.manage.admin.user.ViewUserListForm;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Deal {


	/**
	 * 用户列表
	 * @param request
	 * @param aWebForm
	 */
	public static void userList(HttpServletRequest request,ViewUserListForm aWebForm){

		CTurnPage page = new CTurnPage();
		TUserInfo userPo = new TUserInfo();
		try{
			userPo.setId(aWebForm.getUserId());
			userPo.setLoginName(aWebForm.getLoginName());
			userPo.setName(aWebForm.getUserName());
			userPo.setEducation(aWebForm.getEducation());
			userPo.setRegisteredAddress(aWebForm.getRegisteredAddress());

			page.setQueryList("T_USER_INFO.selectAllByCondition",userPo);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 用户列表
	 * @param request
	 * @param aWebForm
	 */
	public static void userListByJobType(HttpServletRequest request,ViewUserListForm aWebForm){

		CTurnPage page = new CTurnPage();
		TUserInfo userPo = new TUserInfo();
		try{
			userPo.setId(aWebForm.getUserId());
			userPo.setLoginName(aWebForm.getLoginName());
			userPo.setName(aWebForm.getUserName());
			userPo.setEducation(aWebForm.getEducation());
			userPo.setPositionId(request.getParameter("positionId"));

			page.setQueryList("T_USER_INFO.selectByJobType",userPo);
			page.setPerPageRows(10);
			page.excute(request);
			request.setAttribute("page", page);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除用户
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean delUser(HttpServletRequest request,ViewUserListForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUserInfo userPo = new TUserInfo();
		String userId = request.getParameter("userId");
		try{
			if(userId!=null && userId!=""){
				userPo.setId(userId);
				sqlmap.queryForObject("F_DEL_TABLE.delAllAboutUser",userId);
				messageBean.setResult(true);				
			}
		}catch(Exception e){
			messageBean.setMessage(Constants.DEL_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}

	/**
	 *修改求职者密码
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean editUserPass(HttpServletRequest request,EditPassForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUserInfo userPo = new TUserInfo();
		String userId = aWebForm.getUserId();
		System.out.println("eeeeeeeeeeee:"+userId);
		try{
			if(userId!=null && userId!=""){
				userPo.setPassword(aWebForm.getPass());
				userPo.setLoginName(aWebForm.getLoginName());
				sqlmap.update("T_USER_INFO.setPass",userPo);
				messageBean.setResult(true);
			}
			messageBean.setMessage(Constants.EDIT_FAIL);
		}catch(Exception e){
			messageBean.setMessage(Constants.EDIT_FAIL);
			e.printStackTrace();
		}
		return messageBean;
	}
	/**
	 * 加载修改求职者密码信息
	 * @param request
	 * @param aWebForm
	 */
	public static MessageBean loadEditUserPass(HttpServletRequest request,EditPassForm aWebForm) throws SQLException{
		MessageBean messageBean = new MessageBean();
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUserInfo userPo = new TUserInfo();
		String userId = request.getParameter("userId");
		try{


			if(userId!=null && userId!=""){
				userPo.setId(userId);
				userPo = (TUserInfo)sqlmap.queryForObject("T_USER_INFO.selectAllByCondition", userPo);
				aWebForm.setLoginName(userPo.getLoginName());
				aWebForm.setUserId(userId);
				aWebForm.setUserName(userPo.getName());
				messageBean.setResult(true);
			}
		}catch(Exception e){
			messageBean.setMessage(Constants.USER_NOTEXIST);
			e.printStackTrace();
		}
		return messageBean;
	}
}
